﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using App.Domain.Model;

namespace App.DAL.EF
{
    class AppDbContext : DbContext
    {
        internal AppDbContext(string connectionString)
            : base(connectionString)
        {
            // TODO: Complete member initialization
            Database.SetInitializer<AppDbContext>(null);
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<CountryRegion>().ToTable("User.CountryRegion");
            modelBuilder.Entity<CountryRegion>().HasKey(x => x.CountryRegionCode);

            modelBuilder.Entity<StateProvince>().ToTable("User.StateProvince");
            modelBuilder.Entity<StateProvince>().HasKey(x => x.StateProvinceID);
            modelBuilder.Entity<StateProvince>().HasRequired<CountryRegion>(x => x.CountryRegion).WithMany().Map(x => x.MapKey("CountryRegionCode"));

            modelBuilder.Entity<AddressType>().ToTable("User.AddressType");
            modelBuilder.Entity<AddressType>().HasKey(x => x.AddressTypeID);

            modelBuilder.Entity<Address>().ToTable("User.Address");
            modelBuilder.Entity<Address>().HasKey(x => x.AddressID);
            modelBuilder.Entity<Address>().HasRequired<StateProvince>(x => x.StateProvince).WithMany().Map(x => x.MapKey("StateProvinceID"));

            modelBuilder.Entity<AddressMap>().ToTable("User.AddressMap");
            modelBuilder.Entity<AddressMap>().HasKey(x => x.OwnerID);
            modelBuilder.Entity<AddressMap>().HasRequired<Address>(x => x.Address).WithMany().Map(x => x.MapKey("AddressID"));
            modelBuilder.Entity<AddressMap>().HasRequired<AddressType>(x => x.AddressType).WithMany().Map(x => x.MapKey("AddressTypeID"));

            modelBuilder.Entity<Person>().ToTable("User.Person");
            modelBuilder.Entity<Person>().HasKey<int>(x => x.PersonID);

            modelBuilder.Entity<Group>().ToTable("User.Group");
            modelBuilder.Entity<Group>().HasKey<int>(x => x.GroupID);
            modelBuilder.Entity<Group>().HasRequired<Person>(x => x.ContactPerson).WithMany().Map(x => x.MapKey("ContactPersonID"));

            modelBuilder.Entity<EmailAddressMap>().ToTable("User.EmailAddressMap");
            modelBuilder.Entity<EmailAddressMap>().HasKey(x => new { x.EmailAddressID, x.OwnerID });

            modelBuilder.Entity<PhoneNumberType>().ToTable("User.PhoneNumberType");
            modelBuilder.Entity<PhoneNumberType>().HasKey<int>(x => x.PhoneNumberTypeID);

            modelBuilder.Entity<PhoneNumberMap>().ToTable("User.PhoneNumberMap");
            modelBuilder.Entity<PhoneNumberMap>().HasKey(x => x.OwnerID);
            modelBuilder.Entity<PhoneNumberMap>().HasRequired<PhoneNumberType>(x => x.PhoneNumberType).WithMany().Map(x => x.MapKey("PhoneNumberTypeID"));

            modelBuilder.Entity<Role>().ToTable("User.Role");
            modelBuilder.Entity<Role>().HasKey(x => x.RoleID);

            modelBuilder.Entity<Account>().ToTable("User.Account");
            modelBuilder.Entity<Account>().HasKey<string>(x => x.LoginID);
            modelBuilder.Entity<Account>().HasRequired<Role>(x => x.Role).WithMany().Map(x => x.MapKey("RoleID"));

            modelBuilder.Entity<ContactType>().ToTable("User.ContactType");
            modelBuilder.Entity<ContactType>().HasKey<int>(x => x.ContactTypeID);

            base.OnModelCreating(modelBuilder);
        }
    }
}
